import datetime
import http
from fastapi import Query
from models import base_model, data_models
from util.router import router


@router.get("/user_login", tags=['用户'], summary='用户登陆')
async def user_info(name: str = Query(..., min_length=3, max_length=50),
                    pwd: str = Query(..., min_length=3, max_length=200)):
    '''
    用户注册路由
    :param name:
    :param pwd:
    :return:
    '''
    query = base_model.session.query(base_model.UserInfo).filter(base_model.UserInfo.user_name == name).all()
    base_model.session.commit()
    if len(query) == 0:
        return {"code": http.HTTPStatus.UNAUTHORIZED, "msg": "此账号未注册"}
    else:
        if query[0].user_auth_id == 0:
            return {"code": http.HTTPStatus.UNAUTHORIZED, "msg": "登陆失败，账户未审核开通，请联系管理员", "return_code": 0}
        if query[0].user_auth_id == 44:
            if pwd == query[0].user_pwd:
                return {"code": http.HTTPStatus.OK, "msg": "登陆成功", "auth": "营运", "auth_id": query[0].user_auth_id,
                        "return_code": 1}
        if query[0].user_auth_id == 88:
            if pwd == query[0].user_pwd:
                return {"code": http.HTTPStatus.OK, "msg": "登陆成功", "auth": "项目总", "auth_id": query[0].user_auth_id,
                        "return_code": 1}
        if query[0].user_auth_id == 99:
            if pwd == query[0].user_pwd:
                return {"code": http.HTTPStatus.OK, "msg": "登陆成功", "auth": "超级管理员", "auth_id": query[0].user_auth_id,
                        "return_code": 1}
            else:
                return {"code": http.HTTPStatus.UNAUTHORIZED, "msg": "账号或密码错误，登陆失败", "return_code": 0}


@router.post("/user_add", tags=['用户'], summary='添加用户')
async def user_re_add(args: data_models.UserInfo):
    '''
    添加用户路由
    :return:
    '''
    try:
        now_time = datetime.datetime.now().strftime("%Y-%m-%d")
        base_model.session.add(base_model.UserInfo(user_name=args.u_name,
                                                   user_create_time=str(now_time),
                                                   user_pwd=args.u_pwd
                                                   ))
        base_model.session.commit()
        return {"code": http.HTTPStatus.OK, "msg": "添加用户成功", 'u_name': args.u_name, "return_code": 1}
    except:
        base_model.session.rollback()
        print(args)
        return {"code": http.HTTPStatus.OK, "msg": "添加用户失败", "return_code": 0}


@router.post("/user_auth_contro", tags=['用户'], summary='用户权限修改')
async def user_re_add(name: str, auth: data_models.UserAuth):
    '''
    修改用户权限
    :return:
    '''
    query = base_model.session.query(base_model.UserInfo).filter(base_model.UserInfo.user_name == name).all()
    base_model.session.commit()
    if len(query) == 0:
        return {"code": http.HTTPStatus.OK, "msg": "此账号未注册", "return_code": 0}
    else:
        user_auth = base_model.session.query(base_model.UserInfo).filter_by(user_name=name).first()
        user_auth.user_auth_id = auth
        base_model.session.commit()
        return {"code": http.HTTPStatus.OK, "msg": "修改权限成功", 'name': name, 'auth': auth, "return_code": 1}
